package com.atguigu.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * create by undeRdoG on  2021-06-08  14:26
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Spark06_RDD_Operator_Transform1 {

  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setAppName("Operator").setMaster("local[*]")
    val sc = new SparkContext(sparkConf)
    val rdd: RDD[String] = sc.makeRDD(List("hello","spark","scala","flink","hadoop"), 2)

    /**
      * 将每个数据进行分组判断，根据返回的分组key进行分组
      * 相同的key的值会放在同一个组中
      **/
    def groupFunction(str: String):String = {
      // 返回分组的key值
      str.charAt(0).toString
    }


    /**
    *  分组和分区没有必然的关系
    * */

    val res: RDD[(String, Iterable[String])] = rdd.groupBy(groupFunction)

    res.collect().foreach(println)

  }
}
